package com.example.comment_oa.aspact;

import com.alibaba.fastjson.JSONObject;
import com.example.comment_oa.pojos.SysRole;
import com.example.comment_oa.pojos.SysUserRole;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;

/**
 * 数据过滤处理
 *
 * @author ruoyi
 */
@Aspect
@Component
public class DataScopeAspect
{
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    /**
     * 全部数据权限
     */
    public static final String DATA_SCOPE_ALL = "1";

    /**
     * 自定数据权限
     */
    public static final String DATA_SCOPE_CUSTOM = "2";

    /**
     * 部门数据权限
     */
    public static final String DATA_SCOPE_DEPT = "3";

    /**
     * 部门及以下数据权限
     */
    public static final String DATA_SCOPE_DEPT_AND_CHILD = "4";

    /**
     * 仅本人数据权限
     */
    public static final String DATA_SCOPE_SELF = "5";

    /**
     * 数据权限过滤关键字
     */
    public static final String DATA_SCOPE = "dataScope";

    @Before("@annotation(controllerDataScope)")
    public void doBefore(JoinPoint point, DataScope controllerDataScope) throws Throwable
    {
        StringBuilder sqlString = new StringBuilder();
        String roles = stringRedisTemplate.opsForValue().get("roles");
        List<SysUserRole> sysUserRoles = JSONObject.parseArray(roles, SysUserRole.class);
        List<SysRole> roleList=new ArrayList<>();
        sysUserRoles.forEach(e->{
            roleList.add(e.getSysRole());
        });
        SysRole sysRole = roleList.stream().min(Comparator.comparing(SysRole::getDataScope)).get();
        Integer dataScope = sysRole.getDataScope();
        //部门数据
        if(dataScope==3){
            System.out.println("部门数据！");
        }
    }
}
